using Autofac;
using Autofac.Extensions.DependencyInjection;
using CMS.EFCore;
using CMS.Web.AutoFacDI;
using CMS.Web.Filters;
using CMS.Web.Profiles;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews(options => {
    options.Filters.Add<UnitOfWorkFilter>();
    options.Filters.Add<LoginCheckFilter>();
});
/*builder.Services.Configure<MvcOptions>(opt =>
{
    opt.Filters.Add<UnitOfWorkFilter>();
});*/
builder.Services.AddDbContext<MyDbContext>(opt => {
    var connectionString = builder.Configuration.GetSection("ConnectStr").Value;
    opt.UseSqlServer(connectionString);
});
builder.Services.AddMemoryCache();
builder.Services.AddSession();
builder.Services.AddHttpClient();
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory()).ConfigureContainer<ContainerBuilder>(
    containerBuilder => {
        containerBuilder.RegisterModule<AutofacModuleRegister>();
    });
builder.Services.AddAutoMapper(typeof(UserProfile));
var app = builder.Build();
CMS.Common.ServiceProvider.Provider = app.Services;
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment()) {
    app.UseExceptionHandler("/Home/Error");
}

app.UseRouting();
app.UseAuthorization();
app.UseSession();
app.MapStaticAssets();

app.MapControllerRoute(
       name: "default",
       pattern: "{controller=Login}/{action=Index}")
   .WithStaticAssets();


app.Run();